<?php
include_once('Classes/User.php');
include_once('Classes/Student.php');
include_once('Classes/Supervisor.php');
include_once('Classes/Bookkeeper.php');
include_once('Classes/Administrator.php');
include_once('Classes/TimeHelper.php');
include_once('Classes/TimeCard.php');
session_start();
$user = unserialize($_SESSION['User']);
if (!(get_class($user) == "Supervisor" || get_class($user) == "Administrator" || get_class($user) == "Bookkeeper")){echo '<META HTTP-EQUIV="refresh" content="0; url=logout.php">'; die(); }

$time = new TimeHelper();


//if a Supervisor or Admin click "view timecard" take to view_student_timecard.php
$viewTimeCardURL = "view_student_timecard.php";
//if Bookkeepers view timecard, take to bookkeeping.php
if(get_class($user) == "Bookkeeper") $viewTimeCardURL = "bookkeeping.php";


include('Classes/Page.php'); 
$page = new Page("View Users");

isset($_GET['order']) ? $order=$_GET['order'] : $order = "ascending";
isset($_GET['sort']) ? $sort=$_GET['sort'] : $sort = "lName";

// Returns the opposite of ascending/descending given one
function changeOrder($order){
    if ($order=="ascending"){
        $newOrder="descending";
    }
    else {
    	$newOrder="ascending";
    }
    return $newOrder;
}
?>

<h3>View Users</h3>

<table id="table-view">
	<tr>
		<th width="25%" align="center">
		    <a href=<?php 
		            // Dynamically changes the link so that you can view ascending or descending
		       	    if ($sort=="type") $orderLink=changeOrder($order); 
			    else $orderLink="ascending"; 
			    echo "'view_users.php?sort=type&order=".$orderLink."'"; ?>>Type</a> 
		</th>
		
		<th width="25%" align="center">
		    <a href=<?php 
		       	    if ($sort=="username") $orderLink=changeOrder($order); 
			    else $orderLink="ascending"; 
			    echo "'view_users.php?sort=username&order=".$orderLink."'"; ?>>User</a> 
		</th>
		
		<th width="25%" align="center">
		    <a href=<?php 
		       	    if ($sort=="fName") $orderLink=changeOrder($order); 
			    else $orderLink="ascending"; 
			    echo "'view_users.php?sort=fName&order=".$orderLink."'"; ?>>First Name</a> 
		</th>
		
		<th width="25%" align="center">
		    <a href=<?php 
		       	    if ($sort=="lName") $orderLink=changeOrder($order); 
			    else $orderLink="ascending"; 
			    echo "'view_users.php?sort=lName&order=".$orderLink."'"; ?>>Last Name</a> 
		</th>
		
		<th align="center">
			<center>Actions</center>
		</th>
		<th align="center">
			Timecard Status
		</th>
	</tr>
	
	<!--table displaying users-->
	<?php 
	// if there is a message to display (something about a user being
	// added, removed, or edited) display it, and clear it
	if(isset($_SESSION['message'])){
		echo $_SESSION['message'];
	}
	unset($_SESSION['message']);
	// display each user that the user has permission to view
	if (isset($_GET['sort'])){
	    $sort=$_GET['sort'];
	    switch($_GET['sort']){
		case "type":
		     $users = $user->getPermissions("type"); break;
		case "username":
		     $users = $user->getPermissions("username"); break;
		case "lName":
		     $users = $user->getPermissions("lName"); break;
		case "fName":
		     $users = $user->getPermissions("fName"); break;
	    }
	}
	else $users = $user->getPermissions();
	if ($order=="descending") { $users=array_reverse($users);}
	foreach($users as $lowerUser){ 
	
		echo "<tr>";
		echo "<td>".$lowerUser->type."</td>";
		echo "<td>".$lowerUser->username."</td>";
		echo "<td>".$lowerUser->fName."</td>";
		echo "<td>".$lowerUser->lName."</td>";
		echo "<td>";
		
		
		/*
		echo "	<form method='post' action='edit_user.php'>";
		echo "		<input type='hidden' name='username' value='".$lowerUser->username."'/>";
		echo "		<input type='submit' value='Edit'/>";
		echo "	</form>";
		*/
		
		if($lowerUser->type == "Student"){
			echo "	<form method='post' action='".$viewTimeCardURL."'>";
			echo "		<input type='hidden' name='Username' value='".$lowerUser->username."'/>";
			echo "		<input type='submit' value='View Timecard' />";
			echo "	</form>";
		}
		
		$firstDay = $time->firstDayOfWeek(date("m/d/y"));
		
		$timeCard = new TimeCard($lowerUser->username,$firstDay);
		
		
		
		
		if($lowerUser->type == "Student"){
			echo "<td style=";
			$status = $timeCard->getStatus();
			
			switch($status) {
				case "Open":
					echo "'background: #ededfc'";
					break;
				case "Submitted":
					echo "'background:#ebebeb'";
					break;
				case "Approved":
					echo "'background:#caffb5'";
					break;
				case "Rejected":
					echo "'background:#ffd6d6'";
					break;
				default:
					//Check if the status contains "Rejected", or use default case
					if(strstr($status, "Rejected")) echo "'background:#ffd6d6'";
					else echo "'background:#ebebeb'";
			}
			echo ">";
			echo $status;
			echo "</td>";
		}
		//if not a student
		else{
			echo "<td></td>";
		}
		

		
		echo "</td>";
		
		echo "</tr>";
		
		}	
		
	?>
	
</table>

<?php $page->include_footer(); ?>

